//
//  BDFillBusinessInfoStep3ViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/13.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDFillBusinessInfoStep3ViewController.h"
#import "UIViewController+BDAddBusinessHelper.h"
#import "BDUploadShopPhotosRequest.h"
#import "UIImagePickerController+Block.h"
#import "UIImage+Tools.h"
#import "BQActivityView.h"


@interface BDFillBusinessInfoStep3ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *shopPhotoImageView;
@property (weak, nonatomic) IBOutlet UIImageView *cashierPhotoImageView;
@property (weak, nonatomic) IBOutlet UIImageView *corporateIdentityPhotoImageView;

@property (weak, nonatomic) IBOutlet UIButton *shopPhotoBtn;
@property (weak, nonatomic) IBOutlet UIButton *cashierPhotoBtn;
@property (weak, nonatomic) IBOutlet UIButton *corporateIdentityPhoto;


@end

@implementation BDFillBusinessInfoStep3ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.titleButton setTitle:@"店铺资料" forState:UIControlStateNormal];
    [self.rightAreaRBtn setTitle:@"下一步" forState:UIControlStateNormal];
    self.rightAreaRBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [self.rightAreaRBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.rightArea.mas_right);
        make.width.mas_equalTo(@50);
        make.centerY.mas_equalTo(self.rightArea.mas_centerY);
        make.height.mas_equalTo(@30);
    }];
    [self fillbackIfNeed];
}

- (void)fillbackIfNeed{
    if (!self.userShopModel) return;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)handleNavigationAction:(HSYNavigationActionType)actionType{
    if (actionType == HSYNavActionLeftAreaLeft) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    if (actionType == HSYNavActionRightAreaRight) {
        [self goNext:nil];
    }
}

- (void)commitData{
    [self.view endEditing:YES];
    
    if (![self  checkDataIntegrity]) return;
    
    BDUploadShopPhotosRequest *task = [[BDUploadShopPhotosRequest alloc] init];
    task.shopId = [NSString stringWithFormat:@"%ld", (long)self.model.shopId];
    task.fileA = UIImageJPEGRepresentation(self.shopPhotoImageView.image, 0.1);
    task.fileB = UIImageJPEGRepresentation(self.cashierPhotoImageView.image, 0.1);
    task.fileC = UIImageJPEGRepresentation(self.corporateIdentityPhotoImageView.image, 0.1);
    
    [BQActivityView showActiviTy];
    __weak typeof(self)weakSelf = self;
    
    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        [BQActivityView hideActiviTy];
        
        if (responseObject) {
            BDBusinessModel *model = [BDBusinessModel mj_objectWithKeyValues:responseObject];
            weakSelf.model.status = model.status;
            weakSelf.model.auStep = model.auStep;
            //go to next step
            [weakSelf nextStepWihBusinessModel:weakSelf.model userShopModel:weakSelf.userShopModel readOnly:weakSelf.readOnly];
            
        }else{
            if (resultString) {
                [weakSelf showMessage:resultString];
            }
        }
    }];

}

- (IBAction)goNext:(id)sender {
    [self handleNext];
}





-(BOOL)checkDataIntegrity{
    if (!self.shopPhotoImageView.image) {
        [self showMessage:@"请拍摄门头照"];
        return NO;
    }
    
    if (!self.cashierPhotoImageView.image) {
        [self showMessage:@"请拍摄收银台照片"];
        return NO;
    }
    
    if (!self.corporateIdentityPhotoImageView.image) {
        [self showMessage:@"请拍摄法人身份证照片"];
        return NO;
    }
    
    return YES;
}






- (IBAction)shopPhotoBtnClick:(UIButton *)sender {
    if (self.readOnly) return;

    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        [wSelf.shopPhotoBtn setImage:nil forState:UIControlStateNormal];
        wSelf.shopPhotoImageView.image = image;
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}



- (IBAction)cashierPhotoBtnClick:(UIButton *)sender {
    if (self.readOnly) return;

    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        [wSelf.cashierPhotoBtn setImage:nil forState:UIControlStateNormal];
        wSelf.cashierPhotoImageView.image = image;
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}


- (IBAction)corporateIdentityPhotoBtnClick:(UIButton *)sender {
    if (self.readOnly) return;

    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        [wSelf.corporateIdentityPhoto setImage:nil forState:UIControlStateNormal];
        wSelf.corporateIdentityPhotoImageView.image = image;
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
